Q: I can create a movie from a movie file using the NewMovieFromFile API, but how do I create a movie from movie data which resides in memory?A: You can use QuickTime data references to accomplish this. Simply wrap a handle data reference or pointer data reference around the data, then use the NewMovieFromDataRef API to create the movie. Play or otherwise use the movie normally, then dispose of the movie, data reference and data when done. Also, if you know what type of data you are dealing with (MP3, for example) you should add appropriate data reference extensions to indicate the media type. QuickTime uses this information to find the movie importer to use. Technical Note TN1195, 'Tagging Handle Data References in QuickTime 4' describes the data reference extensions in great detail. The code snippet in Listing 1 shows how to create a pointer data reference for data (MP3 data in this case) contained in a NSData data buffer. A data reference extension for file name is also added to the data reference to enable QuickTime to more easily determine which movie importer component to use. The createPointerDataRefWithExtensions routine is taken from Technical Note TN1195, 'Tagging Handle Data References in QuickTime 4'. This pointer data reference is then passed to NewMovieFromDataRef to create the movie: Listing 1: Creating a movie from an NSData data buffer using a pointer data reference. //
// LoadAndPlayMovieFromNSData
//
// Given an NSData data buffer, make a pointer data
// reference then create a movie for the data.
// Optionally add a file name data reference
// extension to the data reference before creating
// the movie.
//
// Parameters
//
// data A data buffer containing the
// data you wish to import
// fileName If you know the original file name
// you should pass it here to help
// QuickTime locate a movie importer
// for the data.
void LoadAndPlayMovieFromNSData(NSData *data, NSString *fileName)
{
Str255 fileName255;
Handle ptrDataRef = NULL;
Movie newMovie = NULL;
c2pstrcpy(fileName255, [fileName UTF8String]);
ptrDataRef = createPointerDataRefWithExtensions(
[data bytes], /* pointer to data */
(Size)[data length], /* data size */
fileName255, /* file name */
0, /* file type */
nil /* mime type string */
);
if (ptrDataRef)
{
short id = 0;
OSErr err = noErr;
/* now create a movie from the data reference */
err = NewMovieFromDataRef(&newMovie,
newMovieActive,
&id,
ptrDataRef,
PointerDataHandlerSubType);
if (err == noErr)
{
// ...play/manipulate your movie here
// when finished, clean up
DisposeMovie(newMovie);
}
DisposeHandle(ptrDataRef);
}
}
Similarly, the code snippet in Listing 2 shows how to create a pointer data reference from a pointer to a block of MP3 data along with data reference extensions for file name. Once again, the pointer data reference is passed to NewMovieFromDataRef to create the movie: Listing 2: Creating a movie using a pointer data reference. //
// createMovieFromMemory
//
// Given a pointer to some movie data, it creates a
// pointer data reference and then calls NewMovieFromDataRef
// with this data reference to create a movie.
//
// Parameters
//
// data A Pointer to your movie data
// dataSize The actual size of the movie data
// specified by the data pointer
// fileName If you know the original file name
// you should pass it here to help
// QuickTime determine which importer
// to use. Pass nil if you do not wish
// to specify the fileName
void createMovieFromMemory(void *data,
Size dataSize,
Str255 fileName)
{
Handle myDataRef = NULL;
myDataRef = createPointerDataRefWithExtensions(
data, /* pointer to data */
dataSize, /* size of data */
fileName, /* file name */
0, /* file type */
nil); /* mime type */
if (myDataRef)
{
OSErr err = noErr;
short id = 0;
Movie newMovie = NULL;
err = NewMovieFromDataRef(
&newMovie,
newMovieActive,
&id,
myDataRef,
PointerDataHandlerSubType);
if (err == noErr)
{
// ... play/manipulate your movie here
// clean up when finished using movie
DisposeMovie(newMovie);
}
// more clean up
DisposeHandle(myDataRef);
}
}
Lastly, the code snippet in Listing 3 shows how to create a handle data reference for MP3 data in a handle along with the same data reference extensions as shown in Listing 2. Again, the createHandleDataRefWithExtensions routine is taken from Technical Note TN1195, 'Tagging Handle Data References in QuickTime 4'. Listing 3: Creating a movie using a handle data reference. //
// createMovieFromHandleMemory
//
// Given a pointer to some movie data, it creates a
// pointer data reference and then calls NewMovieFromDataRef
// with this data reference to create a movie.
//
// Parameters
//
// data A Pointer to your movie data
// dataSize The actual size of the movie data
// specified by the data pointer
// fileName If you know the original file name
// you should pass it here to help
// QuickTime determine which importer
// to use. Pass nil if you do not wish
// to specify the fileName
void createMovieFromHandleMemory(Handle dataHandle,
Str255 fileName)
{
Handle myDataRef = NULL;
myDataRef = createHandleDataRefWithExtensions(
dataHandle, /* data handle */
fileName, /* file name */
0, /* file type */
nil, /* mime type */
nil, /* initialization data */
0); /* init data byte count */
if (myDataRef)
{
OSErr err = noErr;
short id = 0;
Movie newMovie = NULL;
err = NewMovieFromDataRef(
&newMovie,
newMovieActive,
&id,
myDataRef,
HandleDataHandlerSubType);
if (err == noErr)
{
// ... play/manipulate your movie here
// clean up when finished using movie
DisposeMovie(newMovie);
}
// more clean up
DisposeHandle(myDataRef);
}
}
Posted: 2004-05-20 Document Revision HistoryDate | Notes |
---|
2004-05-20 | First Version |
Posted: 2004-05-20
|